package com.example.huhaichao.music.fragment;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.huhaichao.music.R;
import com.example.huhaichao.music.adapter.ViewAdapter;
import com.example.huhaichao.music.util.MusicUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by huhaichao on 2017/5/1.
 */

public class AllMusic extends Fragment {
    private List<String> audioList = new ArrayList<>(); // 要播放的音频列表
    public Callbacks mCallbacks;
    private ViewAdapter viewAdapter;
    private static final String TAG = "AllMusic";
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_list, container, false);
        ListView listview = (ListView) v.findViewById(R.id.musiclist); // 获取布局管理器中添加的ListView组件
        viewAdapter=new ViewAdapter(getActivity(),audioList);
        listview.setAdapter(viewAdapter); // 将适配器与ListView关联
        // 当单击列表项时播放音乐
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> listView, View view,
                                    int position, long id) {
                Log.d(TAG, "onItemClick: "+position);
                mCallbacks.onItemSelected(position);
            }
        });
        return v;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getFiles("/sdcard/"); // 获取SD卡上的全部音频文件
        MusicUtils.initMusicList(audioList);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        //如果activity没有实现Callbacks接口，抛出异常
        if(!(activity instanceof Callbacks))
        {
            throw new IllegalStateException("没有实现接口");
        }
        mCallbacks = (Callbacks) activity;
    }

    //接口
    public interface Callbacks{
        void onItemSelected(int id);
    }

    private void getFiles(String url) {
        File files = new File(url); // 创建文件对象
        File[] file = files.listFiles();
        try {
            for (File f : file) { // 通过for循环遍历获取到的文件数组
                if (f.isDirectory()) { // 如果是目录，也就是文件夹
                    getFiles(f.getAbsolutePath()); // 递归调用
                } else {
                    if (isAudioFile(f.getPath())) { // 如果是音频文件
                        audioList.add(f.getPath()); // 将文件的路径添加到list集合中
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace(); // 输出异常信息
        }
    }

    private static String[] imageFormatSet = new String[] { "mp3" }; // 合法的音频文件格式
    // 判断是否为音频文件
    private static boolean isAudioFile(String path) {
        for (String format : imageFormatSet) { // 遍历数组
            if (path.contains(format)) { // 判断是否为有合法的音频文件
                return true;
            }
        }
        return false;
    }
}
